package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysAlert;
import com.markerhub.service.SysAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 系统告警Controller
 */
@Api(tags = "系统告警管理")
@RestController
@RequestMapping("/api/sys-alert")
public class SysAlertController extends BaseController {

    @Autowired
    private SysAlertService sysAlertService;

    /**
     * 获取告警列表
     */
    @ApiOperation(value = "获取告警列表", notes = "获取所有系统告警信息，支持分页查询")
    @PreAuthorize("hasAuthority('sys:alert:list')")
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) Integer alertType, @RequestParam(required = false) Integer status) {
        // 使用BaseController中的getPage()方法获取分页参数
        Page<SysAlert> page = getPage();
        // 构建查询条件
        QueryWrapper<SysAlert> wrapper = new QueryWrapper<>();
        if (alertType != null) {
            wrapper.eq("alert_type", alertType);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<SysAlert> pageData = sysAlertService.page(page, wrapper);
        return Result.succ(200, "获取系统告警列表成功", pageData);
    }

    /**
     * 查询系统告警
     */
    @ApiOperation(value = "查询系统告警", notes = "根据条件查询系统告警")
    @PreAuthorize("hasAuthority('sys:alert:query')")
    @GetMapping("/query")
    public Result query(@RequestParam(required = false) Integer alertType, @RequestParam(required = false) Integer status) {
        // 使用BaseController中的getPage()方法获取分页参数
        Page<SysAlert> page = getPage();
        // 构建查询条件
        QueryWrapper<SysAlert> wrapper = new QueryWrapper<>();
        if (alertType != null) {
            wrapper.eq("alert_type", alertType);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<SysAlert> pageData = sysAlertService.page(page, wrapper);
        return Result.succ(200, "查询系统告警成功", pageData);
    }

    /**
     * 根据ID获取系统告警信息
     */
    @ApiOperation(value = "获取告警详情", notes = "根据ID获取系统告警详细信息")
    @PreAuthorize("hasAuthority('sys:alert:list')")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        SysAlert alert = sysAlertService.getById(id);
        return Result.succ(200, "获取系统告警信息成功", alert);
    }

    /**
     * 创建系统告警
     */
    @ApiOperation(value = "新增系统告警", notes = "新增系统告警信息")
    @PreAuthorize("hasAuthority('sys:alert:add')")
    @PostMapping("/add")
    public Result add(@RequestBody SysAlert alert) {
        boolean saved = sysAlertService.save(alert);
        return saved ? Result.succ(200, "创建系统告警成功", null) : Result.fail("创建系统告警失败");
    }

    /**
     * 更新系统告警
     */
    @ApiOperation(value = "更新系统告警", notes = "更新系统告警信息")
    @PreAuthorize("hasAuthority('sys:alert:update')")
    @PutMapping("/update")
    public Result update(@RequestBody SysAlert alert) {
        boolean updated = sysAlertService.updateById(alert);
        return updated ? Result.succ(200, "更新系统告警成功", null) : Result.fail("更新系统告警失败");
    }

    /**
     * 删除单个系统告警
     */
    @ApiOperation(value = "删除系统告警", notes = "删除指定ID的系统告警信息")
    @PreAuthorize("hasAuthority('sys:alert:delete')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean deleted = sysAlertService.removeById(id);
        return deleted ? Result.succ(200, "删除系统告警成功", null) : Result.fail("删除系统告警失败");
    }

    /**
     * 批量删除系统告警
     */
    @ApiOperation(value = "批量删除系统告警", notes = "批量删除多个系统告警信息")
    @PreAuthorize("hasAuthority('sys:alert:delete')")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        boolean deleted = sysAlertService.removeByIds(Arrays.asList(ids));
        return deleted ? Result.succ(200, "批量删除系统告警成功", null) : Result.fail("批量删除系统告警失败");
    }

    /**
     * 处理系统告警
     */
    @ApiOperation(value = "处理系统告警", notes = "处理指定的系统告警")
    @PreAuthorize("hasAuthority('sys:alert:update')")
    @PutMapping("/handle")
    public Result handle(@RequestBody HandleAlertDto data) {
        SysAlert alert = sysAlertService.getById(data.getAlertId());
        if (alert == null) {
            return Result.fail("系统告警不存在");
        }
        
        alert.setStatus(data.getStatus());
        alert.setHandler_id(data.getHandlerId());
        alert.setHandle_time(new Date());
        alert.setHandle_remarks(data.getHandleRemarks());
        alert.setUpdated(new Date());
        
        boolean updated = sysAlertService.updateById(alert);
        return updated ? Result.succ(200, "处理系统告警成功", alert) : Result.fail("处理系统告警失败");
    }
    
    // 辅助DTO类
    public static class HandleAlertDto {
        private Long alertId;
        private Integer status;
        private Long handlerId;
        private String handleRemarks;
        
        public Long getAlertId() {
            return alertId;
        }
        
        public void setAlertId(Long alertId) {
            this.alertId = alertId;
        }
        
        public Integer getStatus() {
            return status;
        }
        
        public void setStatus(Integer status) {
            this.status = status;
        }
        
        public Long getHandlerId() {
            return handlerId;
        }
        
        public void setHandlerId(Long handlerId) {
            this.handlerId = handlerId;
        }
        
        public String getHandleRemarks() {
            return handleRemarks;
        }
        
        public void setHandleRemarks(String handleRemarks) {
            this.handleRemarks = handleRemarks;
        }
    }
}